package ipebg.mzk.provider.system.service.impl;

import ipebg.mzk.facade.system.service.DatasourceMonitorService;
import ipebg.mzk.provider.system.dao.impl.DatasourceMonitor;

import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.mchange.v2.c3p0.ComboPooledDataSource;

@Service("datasourceMonitorServiceImpl")
@Transactional
public class DatasourceMonitorServiceImpl implements DatasourceMonitorService {
	private static Log log = LogFactory.getLog(DatasourceMonitorServiceImpl.class);

	@Resource(name = "datasourceMonitor")
	DatasourceMonitor datasourceMonitor;

	@Override
	public Map getSecondLevelCache(String regionName) throws Exception {
		Map map = null;
		try {
			map = datasourceMonitor.getSecondLevelCache(regionName);
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return map;
	}

	@Override
	public double getQueryCacheHitCount() {
		double hitCount = 0;
		try {
			hitCount = datasourceMonitor.getQueryCacheHitCount();
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return hitCount;
	}

	@Override
	public double getQueryCacheMissCount() {
		double missCount = 0;
		try {
			missCount = datasourceMonitor.getQueryCacheMissCount();
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return missCount;
	}

	@Override
	public String[] getEntityNames() {
		String[] entities = null;
		try {
			entities = datasourceMonitor.getEntityNames();
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return entities;
	}

	@Override
	public String[] getSecondLevelCacheRegionNames() {
		String[] regionNames = null;
		try {
			regionNames = datasourceMonitor.getSecondLevelCacheRegionNames();
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return regionNames;
	}

	@Override
	public int getDatasourcePoolSize() {
		int poolSize=0;
		try {
			ComboPooledDataSource ds = (ComboPooledDataSource) datasourceMonitor.getDataSource();
			poolSize = ds.getMaxPoolSize();
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return poolSize;
	}

	@Override
	public int getDatasourcePoolActivesize() {
		int poolSize=0;
		try {
			ComboPooledDataSource ds = (ComboPooledDataSource) datasourceMonitor.getDataSource();
			poolSize = ds.getNumBusyConnections();
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return poolSize;
	}
}
